<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-family: "Open Sans", sans-serif;
            text-transform: uppercase;
            letter-spacing: 3px;
            font-size: 11px;
        }
    </style>
</head>
<body>
<h1>秒杀情况说明 ： </h1>
<p id="secKillStatus" th:text="${errmsg}"></p>
<input id="userId" type="hidden" th:value="${user.getId()}">
<input id="goodsId" type="hidden" th:value="${goodsId}">
</body>
<!--jquery-->
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script>
    $(function () {
        if ($("#secKillStatus").text() === "排队中...") {
            polling();  //会执行countDown()
        }
    });

    function polling() {

        var userId = $("#userId").val();
        var goodsId = $("#goodsId").val();

        //获取剩余时间
        $.get("/seckill/getSecKillResult", {
            user: userId,
            goodsId: goodsId
        }, function (data) {
            var secKillResult = data.obj; //秒杀状态：0排队中，-1秒杀失败，orderId秒杀成功
            console.log("secKillResult==",secKillResult)
            // 排队中
            if (secKillResult == 0 || !secKillResult) {

                setTimeout(polling, 1000); // 1s后再次轮训
                //秒杀失败
            } else if (secKillResult == -1) {

                $("#secKillStatus").text("秒杀失败");
                return true;

            } else {
                // 秒杀成功
                $("#secKillStatus").text("秒杀成功,订单号:" + secKillResult);
                return true;
            }
        })
    }
</script>
</html>